package com.java.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.entity.SugarInfo;
import com.java.entity.event.SugarInfoEvent;
import com.java.mapper.AccountInfoMapper;
import com.java.mapper.SugarInfoMapper;
import com.java.service.SugarService;
import com.java.util.MyMap;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;

@Service
public class SugarServiceImpl implements SugarService {
	
	@Autowired private SugarInfoMapper sugarMapper;
	
	@Autowired private AccountInfoMapper accountMapper;

	@Override
	public TableRespone getSugarList(String accountId, int page, int size) {
		MyMap mm = new MyMap().put("sugarUserId", accountId);
		List<SugarInfoEvent>  list = sugarMapper.selectAll(mm, page, size);
		for (SugarInfoEvent sugarInfoEvent : list) {
			sugarInfoEvent.setAccount(accountMapper.selectByKey(sugarInfoEvent.getSugarUserId()));
		}
		long ll = sugarMapper.selectAllCount(mm);
		return new TableRespone(list, ll);
	}

	@Override
	public BaseRespone getSugarRecord(String accountId, Date date) {
		SugarInfoEvent info = sugarMapper.selectByInfo(new SugarInfo().setSugarUserId(accountId).setSugarDate(date));
		if(null == info) {
			return BaseRespone.error("暂无记录");
		}
		return new BaseRespone(0, "查询成功", info.getSugarValue());
	}

	@Override
	public BaseRespone saveSugarRecord(String accountId, BigDecimal sugarValue, Date date) {
		int row = 0;
		SugarInfo info = sugarMapper.selectByInfo(new SugarInfo().setSugarUserId(accountId).setSugarDate(date));
		if(null == info) {
			info = new SugarInfo();
			info.setSugarId(MyTools.getUUID());
			info.setSugarUserId(accountId);
			info.setSugarDate(date);
			info.setSugarValue(sugarValue);
			info.setSugarTime(new Date());
			row = sugarMapper.insertSelective(info);
		}else {
			info.setSugarValue(sugarValue);
			row = sugarMapper.updateSelective(info);
		}
		return BaseRespone.result(row, "记录成功");
	}

}
